﻿using System;
using CompactMapper.Orm.Annotations;
using CompactMapper.Orm.Contracts;
using CompactMapper.Orm.UnitOfWork.Mappings;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CompactMapper.Orm.Tests
{
    [TestClass]
    public class IndexesTests : OrmInMemoryTest
    {
        [Table]
        public class TableWithIndex : IKeyedPersistent
        {
            [LongPrimaryKey]
            [Index]
            public IKey Key
            {
                get;
                set;
            }
        }

        [TestMethod]
        public void ExploringIndexes()
        {
            const long kiloLines = 1;
            const long kilo = 1000;
            const long numberOfLines = kiloLines * kilo;

            using (var unitOfWork = GetKeyedUnitOfWork<TableWithIndex>())
            {
                for (long i = 0; i < numberOfLines; i++)
                {
                    unitOfWork.MakePersistent(new TableWithIndex { Key = new LongKey(i) });
                }
            }
            using (var unitOfWork = GetKeyedUnitOfWork<TableWithIndex>())
            {
                var keyLong = (long)new Random((int)DateTime.Now.Ticks).Next((int)numberOfLines);
                var key = new LongKey(keyLong);
                Assert.AreEqual(keyLong, unitOfWork.GetByKey(key).Key.Value);
            }
        }

    }
}
